package com.dayuanit.dy15.websocket.controller;

import com.dayuanit.dy15.websocket.dto.ResponseDTO;
import com.dayuanit.dy15.websocket.handler.AtmWebsocketHandler;
import com.dayuanit.dy15.websocket.handler.BaiduHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@RestController
public class MessageController extends BaseController {

    @Autowired
    private AtmWebsocketHandler atmWebsocketHandler;

    @Autowired
    private BaiduHandler baiduHandler;

    @RequestMapping("/websocket/sendMessage.do")
    public ResponseDTO sendMessage(String userId, String message) {
        atmWebsocketHandler.sendMessage(userId, message);
        return ResponseDTO.sucess();
    }

    @RequestMapping("/websocket/voiceText.do")
    public void voiceText(String amount, HttpServletResponse response) {
        byte[] voiceData = baiduHandler.baiduVoice("您有一笔转账金额" + amount + "元已到账。");

        response.setContentType("audio/mp3");
        try {
            OutputStream os = response.getOutputStream();
            os.write(voiceData);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
